﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using HRProject.Code.FAO;
using HRProject.Code.DTO;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using System.IO;

namespace HRProject.Salary
{
    public partial class BasicSalary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Items.Add("tab_navigation_1", "basicsalary");
            Button1.Text = ContentProvider.GetString("ToExcelFile");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Guid selectedJobTitleId;
            int year, month, day;
            try
            {
                selectedJobTitleId = new Guid(Request.Params["jobTitleId"]);
            }
            catch (Exception exp)
            {
                selectedJobTitleId = Guid.Empty;
                Console.Out.WriteLine(exp.Message);
            }

            string strYear = Request.Params["year"];
            if (!int.TryParse(strYear, out year))
            {
                year = DateTime.Now.Year;
            }
            else if ((year < DateTime.MinValue.Year) || (year > DateTime.MaxValue.Year))
            {
                year = DateTime.Now.Year;
            }

            string strMonth = Request.Params["month"];
            if (!int.TryParse(strMonth, out month))
            {
                month = DateTime.Now.Month;
            }
            else if ((month < DateTime.MinValue.Month) || (month > DateTime.MaxValue.Month))
            {
                month = DateTime.Now.Month;
            }

            DateTime date = new DateTime(year, month, 1);

            BasicSalaryDAO basicSalaryDao = new BasicSalaryDAO();
            BLJobTitle jobTitleFilter = new BLJobTitle();
            jobTitleFilter.Id = selectedJobTitleId;
            BLMonthlyBasicSalaryTable basicSalaryTable = basicSalaryDao.Collect(jobTitleFilter, Guid.Empty, string.Empty, date, 1, 2000, 1);

            string templateFile = Server.MapPath("../report/BasicSalary.xlsx");
            string outputFile = Server.MapPath("../report/BasicSalary" + Convert.ToString(DateTime.Now.ToFileTime()) + ".xlsx");
            SalaryExporter exporter = new SalaryExporter(templateFile, outputFile);
            MemoryStream stream = exporter.Export(basicSalaryTable);

            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats";
            Response.AddHeader("Content-Disposition", "attachment; filename="
                + "Report.xlsx");
            // With XLSX it is a bit more complicated as MS Packaging API
            // can't write directly to Response.OutputStream.
            // Therefore we use temporary MemoryStream.
            stream.WriteTo(Response.OutputStream);
            Response.End();
        }
    }
}
